import os
import readline
import string
import sys
import math
import random
from os import system
import re
import pool_plot




def script_pool_tree(output_file, num_iter, num_iter_2,num_inc, extra, num_lin, num_pool, dirname):
	
	line = "echo \"source ('star_script.r')\" | R --save "

	os.system(line);

	line = "echo \"source ('generate_snp_script.r')\" | R --save "

	os.system(line);

	print(int(num_iter/(num_inc)));
	pool_plot.write_pool_data_matrix("pools.txt",output_file, num_iter,num_iter_2, num_inc);

	num_total = int(num_iter_2/num_inc) - int(num_iter/(num_inc));

	line = "echo \"plot.pools.star('pools.txt'," + str(num_total) + ",1)\" | R --save"

	os.system(line);

	write_tree_data("tree.txt",output_file,int(num_iter/num_inc));
	
	for i in range(1,10):
		tree_file = dirname + "Figures/tree_" + str(i)+ "_" + extra + ".ps";
		command = "neato -Tps -Gstart=" + str(i) + " tree.txt" + " -o " + tree_file;
		os.system(command);
#	print(line);
	os.system(line);

	

def read_tree_data(filename, iteration):

	inputfile = open(filename);
	v = inputfile.readlines();

	split_line = [];

	counter_iter = 0;
	check = 0; 
	counter = 0;
	
	data = [];

	for line in v:
		line = line.rstrip('\n');		
		split_line = line.split(' ');
		##print(line);
		if (split_line[0] == "Tree:"):
			check = 1;
		else:
			if ((split_line[0] == '#')&(check == 1)):
				counter_iter = counter_iter + 1;
				check = 0;
			if ((counter_iter == iteration)&(check == 1)):
		##		print(split_line);
				data.append(line);
				data.append('\n');				
				counter = counter + 1;
	return data;	

def read_newick_data(filename, iteration):

	inputfile = open(filename);
	v = inputfile.readlines();

	split_line = [];

	counter_iter = 0;
	check = 0; 
	counter = 0;
	
	data = [];

	for line in v:
		line = line.rstrip('\n');		
		split_line = line.split(' ');
		
		if (split_line[0] == "Newick:"):
			check = 1;
		else:
			if ((split_line[0] == '#')&(check == 1)):
				counter_iter = counter_iter + 1;
				check = 0;
			if ((counter_iter == iteration)&(check == 1)):
				print(split_line);
				data.append(line);
				data.append('\n');				
				counter = counter + 1;
	return data;	

def write_tree_data(out_file, in_file, iteration):
	
	out = open(out_file,'w');
	
	data = [];

	data = read_tree_data(in_file, iteration);

	#print(data);	
	for i in range(0,len(data)):
		out.write(data[i]);

	out.close();


def write_newick_data(out_file, in_file, iteration):
	
	out = open(out_file,'w');
	
	data = [];

	data = read_newick_data(in_file, iteration);

	#print(data);	
	for i in range(0,len(data)):
		out.write(data[i]);

	out.close();



def write_newick_data_iter(out_file, in_file, iter_start, iter_end, iter_len):
	
	out = open(out_file,'w');
	
	data = [];

	for iteration in range(iter_start,iter_end,iter_len):
#		print(iteration);
		data = read_newick_data(in_file, iteration/iter_len);

#		print(data);	
		for i in range(0,len(data)):
			out.write(data[i]);
	out.close();


num_iter = 15000;
num_iter_2 = 187000;
num_inc = 100;
out_file = "test.1.5.out"
num_lin = 5;
num_pools = 10;
pool_plot.write_pool_data_matrix("pools.txt",out_file, num_iter, num_iter_2, num_inc);
write_newick_data_iter("trees.tre",out_file,num_iter,num_iter_2,num_inc);
script_pool_tree(out_file, num_iter,num_iter_2, num_inc, out_file,num_lin, num_pools, "");

command = "Rscript Scripts/plot.newick.input.r test.1.5.ps";
os.system(command);
	
